#include<stdio.h>
struct part{
    int num;
    struct part * next;
};

struct part2{
    int val;
    struct part * next2;
};

struct part * start=NULL,*p,*node,*new;
struct part2 * head = NULL;
int n;


//CREATE
void create()
    {
    int n,i;
    printf("Enter : ");
    scanf("%d",&n);
    if(n==0)
        {
        }

    else if(n==1)
        {
        printf("Enter the value to be added :");
        start=(struct part *)malloc(sizeof(struct part));
        scanf("%d",&start->num);
        start->next=NULL;
        }

    else if(n==2) 
        {
        printf("Enter the value to be added :");
        start=(struct part *)malloc(sizeof(struct part));
        scanf("%d",&start->num);
        start->next=NULL;
        node=start;
        node->next=(struct part *) malloc(sizeof(struct part));
        node=node->next;
        scanf("%d",&node->num);
        node->next=NULL;
        }
    else
        {
        printf("Enter the value to be added :");
        start=(struct part *)malloc(sizeof(struct part));
        scanf("%d",&start->num);
        start->next=NULL;
        node=start;
        node->next=(struct part *) malloc(sizeof(struct part));
        node=node->next;
        scanf("%d",&node->num);
        node->next=NULL;
        for(i=2;i<n;i++)
            {
            node->next=(struct part *) malloc(sizeof(struct part));
            node=node->next;
            scanf("%d",&node->num);
            node->next=NULL;
            }
        }
    }
//APPEND
void append(int append_val)
    {
    node = start->next;
    p = start;
    while(node)
        {
        node = node->next;
        p= p->next;
        }
    new = (struct part* ) malloc(sizeof(struct part));
    new->next = node ;
    p->next = new;
    //printf("Input the value: ");
    //scanf("%d",&new->num);
    new->num=append_val;
    n=n+1;
    }


void extend()
    {
    int lent,i;
    printf("Enter the length of list2 : ");
    scanf("%d",&lent);
    int append_int;
    for(i=0;i<lent;i++)
        {
        scanf("%d",&append_int);
        append(append_int);
        }
    }
    


//DELETE
void delete()
    {
    int val;
    printf("Eneter the number to be deleted : ");
    scanf("%d",&val);
    if(start == NULL)
        {
        printf("the list is empty");
        return;
        }
    struct part *tmp;
    tmp=start;
    struct part *prv;
    prv=NULL;
    while (tmp!=NULL)
        {
        if(tmp->num == val)
        break;
        prv=tmp;
        tmp=tmp->next;
        }
    if(tmp==NULL)
        return;
    if(prv==NULL)
        {
        start=tmp->next;
        return;
        }
    prv->next=tmp->next;
    return;
    }

//INDEX
void index2()
    {
    int val,i=0,flag,x;
    flag=0;
    printf("Enter the value : ");
    scanf("%d",&val);
    node=start;
    while(node)
        {
        if((node->num)==val)
            {
            flag=1;
            break;
            }
        else
            {
            node = node->next;
            }
        i++;
        }
    if(flag==1)
        {
        printf("The index is : %d \n",i+1);
        }
    else
        {
        printf("Number not in list \n");
        }
    }


//REVERSE
void reverse()
    {
    struct part *curr,*tmp;
    curr=NULL;
    while(start!=NULL)
        {
        tmp=start;
        start=start->next;
        tmp->next=curr;
        curr=tmp;
        }
    start=curr;
    }


//COUNT
void count()
    {
    int val,i=0,flag,sum=0;
    flag=0;
    printf("Enter the value : ");
    scanf("%d",&val);
    node=start;
    while(node)
        {
        if(val==(node->num))
            {
            flag=1;
            sum=sum+1;
            node = node->next;
            }
        else
            {
            node = node->next;
            }
        }
    if(flag==1)
        {
        printf("The count is : %d \n",sum);
        }
    else
        {
        printf("Number not in list \n");
        }
    }


//POP
void pop()
    {
    int index_val;
    printf("Enter the index number ; ");
    scanf("%d",&index_val);
    if(start == NULL)
        {
        printf("the list is empty");
        return;
        }
    struct part *tmp;
    tmp=start;
    struct part *prv;
    prv=NULL;
    int count=1;
    while (tmp!=NULL)
        {
        if(count == index_val)
        break;
        prv=tmp;
        tmp=tmp->next;
        count++;
        }
    if(tmp==NULL)
    return;
    if(prv==NULL)
        {
        start=tmp->next;
        return;
        }
    prv->next=tmp->next;
    return;
    }



//SORT
void sort()                          
    {
    int l;
    node=start;
    while(node)
        {
        l++;
        node=node->next;
        }
    int i, j;
    struct  part *p0, *p1 ,*p2;
    p0=start;
    p1=start->next;
    for(i=0;i<l;i++)
        {
        p0=start;
        p1=p0->next;
        for ( j = 1;j<l-i ; j++ )
            {
            if ((p0->num) > (p1->num) )
                {
                p2->num = (p0->num);
                p0->num = p1->num ;
                (p1->num) = p2->num ;
                }
            p0 = p0->next ;
            p1 = p1->next ;
            }  
        }  
    }




//INSERT
void insert()
    {
    int index_val;
    printf("Enter the index number ; ");
    scanf("%d",&index_val);
    int x,i;
    printf("Enter the value to be added : ");
    scanf("%d",&x);
    struct part *curr=NULL;
    curr=(struct part* ) malloc(sizeof(struct part));
    curr->num=x;
    curr->next=NULL;
    if(start==NULL)
        {
        printf("the link list is empty");
        return;
        }
    struct part *tmp,*prv;
    tmp=start;
    prv=NULL;
    int count=1;
    while(tmp!=NULL)
        {
        if(count==index_val)
        break;
        prv=tmp;
        tmp=tmp->next;
        count++;
        }
    if(tmp==NULL)
        {
        prv->next=curr;
        curr->next=NULL;
        return;
        }
    if(prv==NULL)
        {
        curr->next=tmp;
        start=curr;
        return;
        }
    prv->next=curr;
    curr->next=tmp;
    return;
    }


//DISPLAY
void display()
    {
    node = start;
    printf("The list is :  ");
    while (node)
        {
        printf(" %d", node->num);
        node = node->next;
        }
    printf("\n");
    }


//MAIN
void main()
    {
    create();
    display();
    char ch;
    printf("Enter i=insert , a=append , s=sort , r=reverse , n=index , c=count , d=delete , p=pop , e=extend , t=terminate :  \n");
    scanf("%c",&ch);
    scanf("%c",&ch);
    int append_val;
    while(ch!='t')
        {
        switch(ch)
            {
            case 'a':
                printf("Input the value: ");
                scanf("%d",&append_val);
                append(append_val);
                display();
                break;
    
            case 'd':
                delete();
                display();
                break;
            case 'n':
                index2();
                count();
                break;
            case 'i':
                insert();
                display();
                break;
            case 'p':
                pop();
                display();
                break;
            case 'r':
                reverse();
                display();
                break;
            case 'e':
                extend();
                display();
                break;
            case 's':
                sort();                
                display();
                break;
            default:
                printf("Incorrect command enter again \n");
            }
        printf("Enter i=insert , a=append , s=sort , r=reverse , n=index , c=count , d=delete , p=pop , e=extend , t=terminate :  \n");
        scanf("%c",&ch);
        scanf("%c",&ch);
        }
    printf("Final list : ");
    display();
    printf("\n                THANK YOU\n");
    }